import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore('user', () => {
  // 从localStorage获取token,如果没有则为空字符串
  const token = ref(localStorage.getItem('token') || '')

  // 设置token
  function setToken(newToken) {
    token.value = newToken
    localStorage.setItem('token', newToken)
  }

  // 清除token
  function clearToken() {
    token.value = ''
    localStorage.removeItem('token')
  }

  // 清除用户信息
  function clearUserInfo() {
    clearToken()
    localStorage.removeItem('userInfo')
  }

  return { token, setToken, clearToken, clearUserInfo }
}) 